#!/bin/bash
########################################################################
#  Project Magpie. For details, see https://github.com/llnl/magpie.
#
#  Copyright (C) 2020 Intel Corporation. All rights reserved.
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License version
#  2 as published by the Free Software Foundation.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  Aleksander Kantak <aleksander.kantak@intel.com>
########################################################################

# These are functions to be called by magpie-run

source ${MAGPIE_SCRIPTS_HOME}/magpie/exports/magpie-exports-submission-type
source ${MAGPIE_SCRIPTS_HOME}/magpie/exports/magpie-exports-dirs
source ${MAGPIE_SCRIPTS_HOME}/magpie/exports/magpie-exports-user
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-defaults
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-node-identification
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-run
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-log
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-paths

Magpie_run_start_alluxio () {
    if [ "${ALLUXIO_SETUP}" == "yes" ] && [ "${magpie_run_prior_startup_successful}" == "true" ]
    then
        cd ${ALLUXIO_HOME}

        if [ ${MAGPIE_JOB_TYPE} != "setuponly" ]
        then

            if [ "${ALLUXIO_DATA_CLEAR}" == "yes" ]
            then
                echo "Formatting Alluxio"
                ${alluxiocmdprefix}/alluxio format
            fi

            start_log="${ALLUXIO_LOGS_DIR}/alluxio-start.log"
            echo "Starting alluxio"
            ${alluxiocmdprefix}/alluxio-start.sh all ${ALLUXIO_RAMFS_MOUNT_OPTION} | tee ${start_log}

            if grep -q FAILED ${start_log}; then
                magpie_run_alluxio_setup_successful=0
            else
                magpie_run_alluxio_setup_successful=1
            fi

        fi

        echo "*******************************************************"
        echo "*"
        echo "* Alluxio Information"
        echo "*"
        echo "* You can view your Alluxio status by launching a web browser and pointing to ..."
        echo "*"
        echo "* Alluxio UI: http://${ALLUXIO_MASTER_NODE}:${default_alluxio_master_web_port}"
        echo "*"
        echo "*"
        echo "* You can point your Alluxio storage application, ex. Spark, to ..."
        echo "*"
        echo "* Alluxio RPC: alluxio://${ALLUXIO_MASTER_NODE}:${default_alluxio_master_rpc_port}"
        echo "*"
        echo "*"
        echo "* To access Alluxio directly, you'll most likely want to:"
        echo "*"
        echo "*   ${MAGPIE_REMOTE_CMD:-ssh}${MAGPIE_REMOTE_CMD_OPTS:+" "}${MAGPIE_REMOTE_CMD_OPTS} <WORKERNODE>"
        if echo $MAGPIE_SHELL | grep -q csh
        then
            echo "*   setenv JAVA_HOME \"${JAVA_HOME}\""
            echo "*   setenv ALLUXIO_HOME \"${ALLUXIO_HOME}\""
            echo "*   setenv ALLUXIO_CONF_DIR \"${ALLUXIO_CONF_DIR}\""
        else
            echo "*   export JAVA_HOME=\"${JAVA_HOME}\""
            echo "*   export ALLUXIO_HOME=\"${ALLUXIO_HOME}\""
            echo "*   export ALLUXIO_CONF_DIR=\"${ALLUXIO_CONF_DIR}\""
        fi
        echo "*"
        echo "* Then you can do as you please. For example:"
        echo "*"
        echo "*   \$ALLUXIO_HOME/${alluxiocmdprefix}/alluxio fs ls /"
        echo "*"

        if [ "${MAGPIE_JOB_TYPE}" == "setuponly" ]
        then
            echo "* To setup, login and set environment variables per the"
            echo "* instructions above, then run:"
            echo "*"
            echo "*   \$ALLUXIO_HOME/${alluxiocmdprefix}/alluxio format"
            echo "*   \$ALLUXIO_HOME/${alluxiocmdprefix}/alluxio-start.sh all NoMount"
            echo "*"
            echo "* To end/cleanup your session & kill all daemons, login and set"
            echo "* environment variables per the instructions above, then run:"
            echo "*"
            echo "*   \$ALLUXIO_HOME/${alluxiocmdprefix}/alluxio-stop.sh all"
            echo "*"
            magpie_run_alluxio_setup_successful=1
        fi
        if [ "${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}X" != "X" ]
        then
            echo "* If running interactively, sourcing"
            echo "*"
            echo "* ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}"
            echo "*"
            echo "* will set most common environment variables for your job."
            echo "*"
        fi
        echo "*******************************************************"

        if [ "${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}X" != "X" ]
        then
            if echo $MAGPIE_SHELL | grep -q csh
            then
                echo "setenv ALLUXIO_HOME \"${ALLUXIO_HOME}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "setenv ALLUXIO_CONF_DIR \"${ALLUXIO_CONF_DIR}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "setenv ALLUXIO_LOGS_DIR \"${ALLUXIO_LOGS_DIR}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "setenv ALLUXIO_MASTER_NODE \"${ALLUXIO_MASTER_NODE}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "setenv ALLUXIO_MASTER_PORT \"${default_alluxio_master_port}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "setenv ALLUXIO_WORKER_COUNT \"${ALLUXIO_WORKER_COUNT}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "setenv ALLUXIO_VERSION \"${ALLUXIO_VERSION}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
            else
                echo "export ALLUXIO_HOME=\"${ALLUXIO_HOME}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "export ALLUXIO_CONF_DIR=\"${ALLUXIO_CONF_DIR}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "export ALLUXIO_LOGS_DIR=\"${ALLUXIO_LOGS_DIR}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "export ALLUXIO_MASTER_NODE=\"${ALLUXIO_MASTER_NODE}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "export ALLUXIO_MASTER_PORT=\"${default_alluxio_master_port}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "export ALLUXIO_WORKER_COUNT=\"${ALLUXIO_WORKER_COUNT}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "export ALLUXIO_VERSION=\"${ALLUXIO_VERSION}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
            fi
            echo "" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
        fi

        magpie_run_alluxio_should_be_torndown=1
    else
        magpie_run_alluxio_should_be_torndown=0
        magpie_run_alluxio_setup_successful=1
    fi
}

Magpie_run_alluxio () {
    if [ "${ALLUXIO_JOB}" == "testalluxio" ]
    then
        echo "*******************************************************"
        echo "* Running Testalluxio"
        echo "*******************************************************"
        ${MAGPIE_SCRIPTS_HOME}/magpie/run/magpie-run-execute script ${MAGPIE_SCRIPTS_HOME}/magpie/job/magpie-job-alluxio-testalluxio &
        local scriptpid=$!
        Magpie_wait_script_sigusr2_on_job_timeout ${scriptpid}
    else
        Magpie_output_internal_error "ALLUXIO_JOB = ${ALLUXIO_JOB} not handled"
    fi
}

Magpie_run_stop_alluxio () {

    if [ "${ALLUXIO_SETUP}" == "yes" ] && [ "${magpie_run_alluxio_should_be_torndown}" == "1" ]
    then
        if [ ${MAGPIE_JOB_TYPE} != "setuponly" ]
        then
            cd ${ALLUXIO_HOME}

            if [ "${ALLUXIO_DATA_CLEAR}" == "yes" ]
            then
                echo "Clearing alluxio data"
                for x in $(${alluxiocmdprefix}/alluxio fs ls / | awk '{print $9}')
                do
                    command="${alluxiocmdprefix}/alluxio fs rm -R $x"
                    echo "Running $command" >&2
                    $command
                done
            fi

            echo "Stopping alluxio"
            ${alluxiocmdprefix}/alluxio-stop.sh all

            if [ "${ALLUXIO_DATA_CLEAR}" == "yes" ]
            then
                echo "Clearing alluxio underFS dir"
                command="rm -rf ${ALLUXIO_UNDER_FS_DIR}"
                echo "Running $command" >&2
                $command

                if [ "${ALLUXIO_RAMFS_MOUNT_OPTION}" == "Mount" ] || [ "${ALLUXIO_RAMFS_MOUNT_OPTION}" == "SudoMount" ]
                then
                    if [ "${ALLUXIO_RAMFS_MOUNT_OPTION}" == "Mount" ]
                    then
                        umount_cmd="Umount"
                    elif [ "${ALLUXIO_RAMFS_MOUNT_OPTION}" == "SudoMount" ]
                    then
                        umount_cmd="SudoUmount"
                    fi
                    echo "Umounting alluxio ramdisk"
                    command="${alluxiocmdprefix}/alluxio-mount.sh ${umount_cmd} workers"
                    echo "Running $command" >&2
                    $command
                fi
            fi
        fi
    fi
    magpie_run_alluxio_teardown_complete=1
}
